package nl.vu.few.ibivu.biomodels.client;

import java.util.HashMap;

import com.google.gwt.http.client.URL;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.XmlNamespaces;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.FieldType;

public class UniprotDataSource extends DataSource {  

	HashMap<String,String> query = new HashMap<String,String>();
	
	public String getQuery() {
		return query.get("q");
	}

	public void setQuery(String query) {
		if (query == null || query.isEmpty()) {
			this.query.remove("q");
		}
		else {
			this.query.put("q", URL.encode(query));
		}
		setDefaultParams(this.query);
	}

	public UniprotDataSource(String id) {
		setID(id);
        setDataFormat(DSDataFormat.XML);
        
        XmlNamespaces namespaces = new XmlNamespaces();
        namespaces.addNamespace("up","http://uniprot.org/uniprot");
        setXmlNamespaces(namespaces);
        
        setRecordXPath("/up:uniprot/up:entry");
        
        DataSourceField nameField =
        	new DataSourceField("id", FieldType.TEXT, "ID");
        nameField.setValueXPath("up:name");
        nameField.setPrimaryKey(true);
        
        DataSourceField accessionField =
        	new DataSourceField("accession", FieldType.TEXT, "Accession");
        accessionField.setValueXPath("up:accession");
        accessionField.setMultiple(true);
        
        DataSourceField geneField =
        	new DataSourceField("geneName", FieldType.TEXT, "Name");
        geneField.setValueXPath("up:gene/up:name");
        geneField.setMultiple(true);
        
        DataSourceField shortNameField =
        	new DataSourceField("shortName", FieldType.TEXT, "Protein Name");
        shortNameField.setValueXPath("up:protein/up:recommendedName/up:shortName");
        shortNameField.setHidden(true);
        
        DataSourceField longNameField =
        	new DataSourceField("fullName", FieldType.TEXT, "Protein Full Name");
        longNameField.setValueXPath("up:protein/up:recommendedName/up:fullName");
        longNameField.setDetail(true);
        
        DataSourceField organismField =
        	new DataSourceField("organismName", FieldType.TEXT, "Organism Name");
        organismField.setValueXPath("up:organism/up:name[@type='scientific']");
        
        setFields(nameField, accessionField, geneField, shortNameField, longNameField ,organismField);
        setDataURL("/biomodels/uniprot");
        setDropExtraFields(true);
        // The following has to be uncommented if we don't want
        // to retrieve namespace information
        //query.put("r", "yes");
        setDefaultParams(query);
    }
    
    
}
